package com.example.qianfeng.innerclass;

/**
 * 内部类
 * 1. 成员内部类，直接在类中定义一个内部类
 * 2. 方法内部类，通过方法来创建一个内部类
 */
public class TestInner {
    public static void main(String[] args) {
        OutClass out = new OutClass();
        // 构建成员内部类,通常不建议这样初始化
        OutClass.InnerClass innerClass = out.new InnerClass();
        out.setUserName("admin");
        innerClass.setAge(18);

        // 通过方法获取到内部类
        out.showInnerByMethod();

    }
}

class OutClass {
    private String userName;

    //方法内部类
    public void showInnerByMethod() {
        class InnerClass2 {
            public void print() {
                System.out.println("this is innerClass2");
            }
        }
        InnerClass2 innerClass2 = new InnerClass2();
        innerClass2.print();
    }

    // 成员内部类
    class InnerClass {
        private int age;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
